﻿using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

using Tools.Json;

using SY.WebSite.BLL;
using SY.Uap.Entity;

namespace SY.WebSite.Web.Handler
{
    public class UserManageHandler : AdminWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "getUserList":
                    GetUserList();
                    break;
                case "reset":
                    ResetPassword();
                    break;
                case "edit":
                    EditUserCommonInfo();
                    break;
                case "addUser":
                    AddUser();
                    break;
                case "setStatus":
                    SetStatus();
                    break;
                case "setUserType":
                    SetUserType();
                    break;
            }

            context.Response.End();
        }

        public void SetUserType()
        {
            int userId = Tools.Web.Request.GetFormInt("userId", 0);
            int userType = Tools.Web.Request.GetFormInt("userType", 0);

            if (userId <= 0 || userType < 0)
                BreakResponse();

            User user = BizUser.GetByKey(userId);
            if (user == null)
                BreakResponse();

            user.UserType = userType;

            int result = BizUser.UpdateUserType(user);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器繁忙，请稍候再试");
        }

        public void SetStatus()
        {
            int userId = Tools.Web.Request.GetFormInt("userId", 0);
            int status = Tools.Web.Request.GetFormInt("status", 0);

            if (userId <= 0 || status < 0)
                BreakResponse();

            User user = BizUser.GetByKey(userId);
            if (user == null)
                BreakResponse();

            user.Status = status;

            int result = BizUser.UpdateStatus(user);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器繁忙，请稍候再试");
        }

        public void AddUser()
        {
            long jobNumber = Tools.Web.Request.GetFormLong("jobNumber", 0);
            string userName = Tools.Web.Request.GetFormString("userName");
            string password = Tools.Web.Request.GetFormString("password");
            string deptName = Tools.Web.Request.GetFormString("deptName");
            string jobName = Tools.Web.Request.GetFormString("jobName");
            long phone = Tools.Web.Request.GetFormLong("phone", 0);
            int gender = Tools.Web.Request.GetFormInt("gender", 0);
            string email = Tools.Web.Request.GetFormString("email");

            User user = new User
                            {
                                JobNumber = jobNumber,
                                CreateBy = CurrentUser.UserId,
                                DeptName = deptName,
                                Email = email,
                                Gender = gender,
                                Password = password,
                                JobName = jobName,
                                Phone = phone,
                                UserName = userName,
                                UserType = 0,
                                Status = 0
                            };
            if (BizUser.CheckJobNumberIfExists(jobNumber))
            {
                BreakResponse("工号已存在");
            }

            if (BizUser.Add(user) > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器繁忙，请稍候再试");
        }

        public void EditUserCommonInfo()
        {
            int userId = Tools.Web.Request.GetFormInt("userId", 0);

            if (userId <= 0)
                BreakResponse();

            User user = BizUser.GetByKey(userId);
            if (user == null)
                BreakResponse();

            string deptName = Request.Form["deptName"];
            string jobName = Request.Form["jobName"];
            long phone = Tools.Web.Request.GetFormLong("phone", 0);
            int gender = Tools.Web.Request.GetFormInt("gender", 0);
            string email = Request.Form["email"];

            if (gender>0)
                user.Gender = gender;
            if(!string.IsNullOrEmpty(deptName))
                user.DeptName = deptName;
            if (!string.IsNullOrEmpty(jobName))
                user.JobName = jobName;
            if (phone>0)
                user.Phone = phone;
            if (!string.IsNullOrEmpty(email))
                user.Email = email;

            int result = BizUser.UpdateCommonInfo(user);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器繁忙，请稍候再试");
        }

        public void ResetPassword() {
            int userId = Tools.Web.Request.GetFormInt("userId", 0);
            string newPassword = Tools.Web.Request.GetFormString("newPassword");

            if (userId <= 0 || string.IsNullOrEmpty(newPassword))
                BreakResponse();

            User user = BizUser.GetByKey(userId);
            if (user == null)
                BreakResponse();

            user.Password = newPassword;
            int result = BizUser.UpdatePasswordForcedly(user);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器繁忙，请稍候再试");
        }

        public void GetUserList()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);

            string userName = RequestStringParam("user");
            string deptName = RequestStringParam("deptName");
            string jobName = RequestStringParam("jobName");
            int status = RequestIntParam("status", 100);
            int gender = RequestIntParam("gender", 100);
            int userType = RequestIntParam("usertype", 200);

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Hashtable> list = BizUser.LoadUserListWithFatProperty();

            if (!string.IsNullOrEmpty(userName))
                list = list.Where(l => l["UserName"].ToString().Contains(userName) || l["JobNumber"].ToString().Contains(userName));

            if (!string.IsNullOrEmpty(deptName))
                list = list.Where(l => l["DeptName"].ToString().Contains(deptName));

            if (!string.IsNullOrEmpty(jobName))
                list = list.Where(l => l["JobName"].ToString().Contains(jobName));

            if (status != 100)
                list = list.Where(l => int.Parse(l["Status"].ToString()) == status);

            if (gender != 100)
                list = list.Where(l => int.Parse(l["Gender"].ToString()) == gender);

            if (userType != 200)
                list = list.Where(l => int.Parse(l["UserType"].ToString()) == userType);

            jt.Add("list", list.Skip(pageIndex*pageSize).Take(pageSize));
            jt.Add("count", list.Count());
            Response.Write(jt);
        }
    }
}